<?php
class TTT_Myfilter_Block_Adminhtml_Menu extends Mage_Adminhtml_Block_Page_Menu
{
	private $_customFilterMenuKey = array('customerfilter', 'productfilter', 'orderfilter');


	protected function _buildMenuArray(Varien_Simplexml_Element $parent=null, $path='', $level=0)
	{
		if (is_null($parent)) {
			$parent = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('menu');
		}

		$parentArr = array();
		$sortOrder = 0;
		foreach ($parent->children() as $childName => $child) {
			if (1 == $child->disabled) {
				continue;
			}

			$aclResource = 'admin/' . ($child->resource ? (string)$child->resource : $path . $childName);
			if (!$this->_checkAcl($aclResource)) {
				continue;
			}

			if ($child->depends && !$this->_checkDepends($child->depends)) {
				continue;
			}

			$menuArr = array();

			$menuArr['label'] = $this->_getHelperValue($child);

			$menuArr['sort_order'] = $child->sort_order ? (int)$child->sort_order : $sortOrder;

			if ($child->action) {
				$menuArr['url'] = $this->_url->getUrl((string)$child->action, array('_cache_secret_key' => true));
			} else {
				$menuArr['url'] = '#';
				$menuArr['click'] = 'return false';
			}

			$menuArr['active'] = ($this->getActive()==$path.$childName)
			|| (strpos($this->getActive(), $path.$childName.'/')===0);

			$menuArr['level'] = $level;

			if ($child->children) {
				if (in_array($childName, $this->_customFilterMenuKey)) {
					$customMenuCollection = $this->_getMenuCollectionForEntity($childName);
					$size = $customMenuCollection->getSize();
					if ($size) {
						$i = 0;
						foreach ($customMenuCollection as $menu) {
							if (++$i == $size) {
								$menuArr['children'] ['custom_'. $menu->getId()] = array('label' => $menu->name, 'url' => $menu->getUrl(), 'level' =>2, 'last'=> true);
							} else {
								$menuArr['children'] ['custom_'. $menu->getId()] = array('label' => $menu->name, 'url' => $menu->getUrl(), 'level' =>2);
							}
						}
					}
				} else {
					$menuArr['children'] = $this->_buildMenuArray($child->children, $path.$childName.'/', $level+1);
				}
			}
			$parentArr[$childName] = $menuArr;
			$sortOrder++;
		}

		uasort($parentArr, array($this, '_sortMenu'));

		while (list($key, $value) = each($parentArr)) {
			$last = $key;
		}
		if (isset($last)) {
			$parentArr[$last]['last'] = true;
		}

		return $parentArr;
	}


	/*
	 * Create New Varien_Simplexml_Element and sets its childs so that menu can be built
	 * @params $childName string
	 * @params $xmlNode Varien_Simplexml_Element
	 */
	private function _getMenuCollectionForEntity($childName)
	{
		$entity = '';
		switch ($childName) {
			case 'customerfilter':
				$entity = TTT_Myfilter_Model_Entity::CUSTOMER;
				break;
			case 'productfilter':
				$entity = TTT_Myfilter_Model_Entity::PRODUCT;
				break;
			case 'orderfilter':
				$entity = TTT_Myfilter_Model_Entity::ORDER;
				break;
		}

		$myfilterCollection = Mage::getModel('myfilter/myfilter')
							->getCollection()
							->addFieldToFilter('entity', $entity)
							->addFieldToFilter('menu', 1);

		return $myfilterCollection;
	}

}